package com.sls.api.util;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

/**
 * 定时删除文件夹及其下的所有文件
 * 
 * @author cx
 * 
 */
public class FileTimer {
	private static Timer timer = null;
	private static Logger log = Logger.getLogger(FileTimer.class);

	/**
	 * 定时对文件夹及其下的所有文件
	 * 
	 * @param n
	 */
	public static void startTimer() {

		timer = new Timer();
		log.info("定时器已开始启动...");

		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				Date date = new Date();
				SimpleDateFormat format = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String now = format.format(date);

				SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMdd");
				String daysStr = PropertiesUtil.getValue("server.http.days");
				int days = 0;
				try {
					if (StringUtils.isNotEmpty(daysStr)) {
						days = Integer.parseInt(daysStr.trim());
					}
				} catch (NumberFormatException e) {
					days = 5;
				}
				long dayTime = days * 24 * 3600 * 1000;
				long time = date.getTime() - dayTime; // n天之前的时间
				String yesDay = format1.format(time);
				String path = PropertiesUtil.getValue("bill.project.path");
				String folderName = PropertiesUtil.getValue("bill.folder.name");
				File file1 = new File(path + File.separator + folderName
						+ File.separator + yesDay);
				if (file1.exists()) {
					deleteDirectory(file1.getAbsolutePath());
					// deleteAll(file1);
					// file1.delete();//删除该文件夹
				}
				log.info("定时器启动完成..." + now);
			}
		}, getTimeLength(), 24 * 3600 * 1000);

	}

	/*
	 * public static void deleteAll(File file) { if (file.isFile() ||
	 * file.list().length == 0) { file.delete(); } else { File[] files =
	 * file.listFiles(); for (File f : files) { deleteAll(f);// 递归删除每一个文件 } } }
	 */

	/**
	 * 删除文件夹
	 * 
	 * @param filePath
	 */
	private static void deleteDirectory(String filePath) {
		String cmdStr = "";
		String os = getOS();
		if (os.contains("Windows")) {
			cmdStr = "cmd /c rmdir /q/s " + filePath;
		} else if (os.contains("Linux")) {
			cmdStr = "rm -rf " + filePath;
		}
		final String cmd = cmdStr;
		Process p = null;
		try {
			p = Runtime.getRuntime().exec(cmd);
			int i = p.waitFor();// 挂起等待
			if (i != 0) {
				log.error("命令执行失败!");
			}
			if (p != null) {
				p.destroy();
				p = null;
			}
		} catch (Exception e) {
			log.error(e);
		}
	}

	/**
	 * 判断操作系统
	 * 
	 * @return
	 */
	private static String getOS() {
		String os = "";
		if ("\\".equals(File.separator)) {
			os = "Windows";
		} else if ("/".equals(File.separator)) {
			os = "Linux";
		}
		return os;
	}

	/**
	 * 当前时刻距离今天最后时刻的毫秒数
	 */
	private static long getTimeLength() {
		long value = 0;
		Date d = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		String now = format.format(d);
		String endDay = now + "235959"; // 今天最后时刻

		SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");

		try {
			Date endDate = format1.parse(endDay);
			long dateTime = d.getTime();
			long endDateTime = endDate.getTime();
			value = endDateTime - dateTime;
		} catch (ParseException e) {
			log.error(e);
		}

		return value;
	}

}
